package servlet.user;

import dto.UserModelDto;
import model.ResultModel;
import model.UserModel;
import net.sf.json.JSONArray;
import service.IUserService;
import service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.ResultSet;

@WebServlet("/login")
public class UserLoginServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //乱码
        req.setCharacterEncoding("UTF-8");//请求编码格式为utf-8
        resp.setCharacterEncoding("UTF-8");//响应编码格式为utf-8
        resp.setContentType("text/json;charset=utf-8");//解决传值乱码
        //拿页面的值
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String code = req.getParameter("code");
        String sCode = req.getSession().getAttribute("code").toString();

        //封装
        UserModelDto user = new UserModelDto();
        user.setUsername(username);
        user.setPassword(password);
        user.setCode(code);
        user.setsCOde(sCode);
        //服务逻辑层
        IUserService service = new UserServiceImpl();
        ResultModel resultModel = service.loginResult(user);

        if (resultModel.getCode().equals("200")) {
            req.getSession().setAttribute("username", username);
        }
        //返回数据
        JSONArray jsonArray = JSONArray.fromObject(resultModel);
        resp.getWriter().println(jsonArray);

    }
}
